package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.Wallet;

import java.util.List;

/**
 * @author wenqin 2017-05-05 10:41
 */
public class WalletAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_TOP = -1;
    private LayoutInflater mInflater;
    private List<Wallet> mWalletList;
    private OnItemClickListener mOnItemClickListener;
    private OnWalletTopViewClickListener mOnWalletTopViewClickListener;

    public WalletAdapter(Context context, List<Wallet> walletList) {
        mWalletList = walletList;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TOP) {
            return new TopVh(mInflater.inflate(R.layout.include_wallet_top, parent, false));
        }
        return new Vh(mInflater.inflate(R.layout.adapter_wallet, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) != TYPE_TOP) {
            Vh normalViewHolder = (Vh) holder;
            normalViewHolder.mTitle.setText(mWalletList.get(position).getTitle());

            if (!TextUtils.isEmpty(mWalletList.get(position).getPrice())) {
                normalViewHolder.mPrice.setText("¥ " + mWalletList.get(position).getPrice() + "");
            }
            normalViewHolder.mIcon.setImageResource(mWalletList.get(position).getIcon());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mWalletList == null ? 0 : mWalletList.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        ImageView mIcon;
        TextView mTitle;
        TextView mPrice;

        public Vh(View itemView) {
            super(itemView);
            mIcon = itemView.findViewById(R.id.icon);
            mTitle = itemView.findViewById(R.id.title);
            mPrice = itemView.findViewById(R.id.price);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.itemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    class TopVh extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView mWithdrawView;
        TextView mBankView;
        TextView mTradingPwdView;

        TopVh(View itemView) {
            super(itemView);
            mWithdrawView = itemView.findViewById(R.id.withdraw_view);
            mBankView = itemView.findViewById(R.id.bank_view);
            mTradingPwdView = itemView.findViewById(R.id.trading_pwd_view);
            mWithdrawView.setOnClickListener(this);
            mBankView.setOnClickListener(this);
            mTradingPwdView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnWalletTopViewClickListener == null) {
                return;
            }
            switch (v.getId()) {
                case R.id.withdraw_view:
                    mOnWalletTopViewClickListener.onWithdrawView();
                    break;
                case R.id.bank_view:
                    mOnWalletTopViewClickListener.onBankView();
                    break;
                case R.id.trading_pwd_view:
                    mOnWalletTopViewClickListener.tradingPwdView();
                    break;
                default:

                    break;
            }
        }
    }

    public interface OnItemClickListener {
        void itemClick(int position);
    }

    public interface OnWalletTopViewClickListener {
        void onBankView();

        void onWithdrawView();

        void tradingPwdView();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnWalletTopViewClickListener(OnWalletTopViewClickListener
                                                        onWalletTopViewClickListener) {
        mOnWalletTopViewClickListener = onWalletTopViewClickListener;
    }
}
